package cn.blogger.controller;

import cn.blogger.entity.PageResult;
import cn.blogger.entity.QueryPageBean;
import cn.blogger.pojo.TCategory;
import cn.blogger.entity.Result;
import cn.blogger.service.CategoryService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {
    @Reference
    private CategoryService categoryService;

    //新增标签
    @PostMapping ("/add.do")
    public Result add(@RequestBody TCategory tCategory){
        try {
            categoryService.insert(tCategory);
        }catch (Exception e){
            return new Result(false, "新增标签失败");
        }
        return new Result(true, "新增标签成功");
    }
    //查询所有标签
    @GetMapping("/selectAll.do")
    public List<TCategory> all(@RequestParam(value = "currentPage") Integer currentPage,
                               @RequestParam(value = "pageSize") Integer pageSize){
        return categoryService.listAll(currentPage,pageSize);
    }

    //更新标签
    @PutMapping("/update.do")
    public Result update(@RequestBody TCategory tCategory){
        try {
            categoryService.update(tCategory);
        }catch (Exception e){
            return new Result(false,"标签更新失败");
        }
        return new Result(true,"标签更新成功");
    }
    // 删除标签
    @DeleteMapping("/delete.do")
    public Result delete(@RequestParam(value = "uid") Integer uid){
        try {
            categoryService.deleteById(uid);
        }catch (Exception e){
            return new Result(false,"标签删除失败");
        }
        return new Result(true,"标签删除成功");
    }
}